//
//  TLCommentCell.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/9/20.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//

#import "TLCommentCell.h"
#import "CatZanButton.h"
#import "AVOSUserManger.h"
#import "AVOSManger.h"
#import "UIImage+Circle.h"


@implementation TLCommentCell
@synthesize cmtObjId;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    return self;
}

- (void)awakeFromNib {
    self.supportBtn = [[CatZanButton alloc] initWithFrame:CGRectMake(0, 0, self.supportBtnFrame.frame.size.width, self.supportBtnFrame.frame.size.height)];
    
    [self.supportBtnFrame addSubview:self.supportBtn];
    // Initialization code
    [self.supportBtn setType:CatZanButtonTypeFirework];
    
     __weak typeof(self) weakSelf = self;
    
    [self.supportBtn setClickHandler:^(CatZanButton *zanButton) {
        if (zanButton.isZan) {
            [[AVOSManger sharedManger] supportCmtId:weakSelf.cmtObjId completion:^(NSUInteger sptNum, BOOL succ) {
                    NSLog(@"点击评论的赞!");
                if(succ)
                {
                    int preZan = [[weakSelf.supportLabel text] intValue];
                    
                    preZan ++;
                    
                    weakSelf.supportLabel.text = [NSString stringWithFormat:@"%d",preZan];
                    
                }
            }];

        }else{
            [[AVOSManger sharedManger]  cancleCmtId:weakSelf.cmtObjId completion:^(NSUInteger sptNum, BOOL succ) {
                if(succ)
                {
                    int preZan = [[weakSelf.supportLabel text] intValue];
                    
                    preZan --;
                    
                    weakSelf.supportLabel.text = [NSString stringWithFormat:@"%d",preZan];
                    
                }
            }];
        }
    }];
    
    CGSize newSize = [self.userComment sizeThatFits:self.userComment.frame.size];
    
    self.cmtHConstant.constant = newSize.height;
}

- (void)fetchImgWithName:(NSString *)name{
    [AVOSUserManger getUserFaceByUserName:name completion:^(UIImage *head, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if(!error)
            self.headerThumb.image = [head circleImageBordWidth:2];
            else
            self.headerThumb.image = [[UIImage imageNamed:@"DefaultHead"] circleImageBordWidth:2];
        });
    }];
}

- (void)layoutSubviews{
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
